<template>
  <div class="detail">
    <div class="search__filter">
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">应用:</div>
          <div class="input">
            <el-select v-model="searchFilter.proID" placeholder="请选择" @change="search">
              <el-option v-for="item in proList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">错误类型:</div>
          <div class="input">
            <el-select v-model="searchFilter.errType" placeholder="请选择" @change="search">
              <el-option v-for="item in errTypeList" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </div>
        </div>
      </SearchBox>
    </div>
    <div class="list__wrapper">
      <el-table v-loading="loading" :data="tableData" element-loading-background="rgba(0, 0, 0, 0.8)">
        <el-table-column type="index" width="50" />
        <el-table-column prop="prodID" label="所属应用">
          <template slot-scope="scope">
            {{ findProd(scope.row.proID) }}
          </template>
        </el-table-column>
        <el-table-column prop="errType" label="错误类型" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.errType == 1" type="danger">{{ errTypeList.find(i => i.value ==
              scope.row.errType).label
            }}</el-tag>
            <el-tag v-if="scope.row.errType == 2">{{ errTypeList.find(i => i.value == scope.row.errType).label
            }}</el-tag>
            <el-tag v-if="scope.row.errType == 3" type="warning">{{ errTypeList.find(i => i.value ==
              scope.row.errType).label
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="page" label="发生页面" width="200">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" placement="top">
              <div slot="content" class="tip-content">{{ scope.row.page }}</div>
              <p class="ellipsis">{{ scope.row.page }}</p>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="message" label="报错信息" width="200">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" placement="top">
              <div slot="content" class="tip-content">{{ scope.row.message }}</div>
              <p class="ellipsis">{{ scope.row.message }}</p>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="target" label="目标地址" width="200">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" placement="top">
              <div slot="content" class="tip-content">{{ scope.row.target }}</div>
              <p class="ellipsis">{{ scope.row.target }}</p>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status == 0"
              size="mini"
              type="primary"
              @click="handleData(scope.row)"
            >处理</el-button>
            <el-button size="mini" type="primary" @click="toDetail(scope.row)">详情</el-button>
            <el-button size="mini" type="danger" @click="delData(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="page"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </div>
    <AddEdit v-model="addShow" :type-list="typeList" :current-item="currentItem" @close="closeDia" @reflash="getList" />
    <el-dialog title="详情" :visible.sync="detailShow" width="50%" top="5vh" :before-close="() => { detailShow = false }">
      <json-viewer :value="detailJson" :expand-depth="10" />
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';
import { getToken } from '@/utils/auth';
import Pagination from '@/components/Pagination';
import { debounce } from '@/utils/index';
import AddEdit from './components/AddEdit.vue'
export default {
  name: 'MonitorProject',
  components: {
    Pagination,
    AddEdit
  },
  data() {
    return {
      detailJson: {},
      detailShow: false,
      addShow: false,
      currentItem: null,
      addActive: true,
      delActive: false,
      form: {
        remark: ''
      },
      dialogVisible: false,
      froms: [
        {
          label: '解决方案',
          value: 1
        },
        {
          label: '联系我们',
          value: 2
        }
      ],
      errTypeList: [
        {
          label: '代码错误',
          value: 1
        },
        {
          label: '接口错误',
          value: 2
        },
        {
          label: '静态资源错误',
          value: 3
        }
      ],
      searchFilter: {
        proID: '',
        errType: ''
      },
      page: 1,
      pageSize: 10,
      tableData: null,
      totalSize: 0,
      loading: false,
      currData: null,
      typeList: [{
        label: 'H5/PC/Web项目',
        value: '1'
      },
      {
        label: 'Uni-App',
        value: '2'
      }],
      proList: []
    };
  },
  computed: {
    databaseName() {
      return getToken('databaseName');
    }
  },
  created() {
    this.getProList()
  },
  methods: {
    findProd(id) {
      const { proList } = this
      const find = proList.find(i => i.id == id)
      if (find) {
        return find.name
      } else {
        return ''
      }
    },
    handleData(data) {
      this.currData = data;
      this.dialogVisible = true;
    },
    debounceSearch: debounce(function () {
      this.search();
    }, 250),
    search() {
      this.page = 1;
      this.getList();
    },
    getProList() {
      const { searchFilter } = this
      axios({
        method: 'post',
        url: this.$webSerUrlPro + 'config/project/queryByCondition',
        headers: {
          databaseName: this.databaseName
        },
        data: searchFilter
      }).then(res => {
        this.proList = res.data.data
        this.getList()
      }).finally(() => { })
    },
    async getList() {
      const { searchFilter, page, pageSize } = this;
      this.loading = true;
      const res = await axios({
        method: 'post',
        url: this.$webSerUrlPro + 'config/statistic/queryPageList',
        headers: {
          databaseName: this.databaseName
        },
        data: {
          page,
          size: pageSize,
          entity: searchFilter
        }
      });
      this.loading = false;
      this.totalSize = res.data.data.totalElements;
      this.tableData = res.data.data.content.map(i => {
        return {
          ...i,
          ...JSON.parse(i.content)
        }
      });
      if (res.data.data.content.length === 0 && this.page !== 1) {
        this.currentPage--
        this.getList()
      }
    },
    editData(currData) {
      this.currentItem = JSON.parse(JSON.stringify(currData));
      this.addShow = true
    },
    delData(currData) {
      this.$confirm('是否确定此操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await axios({
            method: 'get',
            url: this.$webSerUrlPro + 'config/statistic/del',
            headers: {
              databaseName: this.databaseName
            },
            params: {
              id: currData.id
            }
          });
          this.getList();
        })
        .catch(() => { });
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getList();
    },
    changeCurrentPage(currentPage) {
      this.page = currentPage;
      this.getList();
    },
    closeDia() {
      this.addShow = false
      setTimeout(() => {
        this.currentItem = null
      }, 250);
    },
    toDetail(row) {
      const rowCopy = JSON.parse(JSON.stringify(row))
      delete rowCopy.content
      delete rowCopy.type
      delete rowCopy.id
      delete rowCopy.errType
      this.detailJson = rowCopy
      this.detailShow = true
    }
  }
};
</script>

<style lang="scss" scoped>
.tip-content {
  max-width: 200px;
}

.ellipsis {
  white-space: nowrap;
  /* 防止文本换行 */
  overflow: hidden;
  /* 隐藏超出部分 */
  text-overflow: ellipsis;
  /* 显示省略号 */
}

::v-deep {
  .el-table {
    height: calc(100% - 42px);
  }

  .el-table__body-wrapper {
    height: calc(100% - 44px) !important;
    overflow-y: auto;
  }
}

.detail {
  height: 100%;

  .search__filter {
    background: #00171c;
    flex-shrink: 0;
    padding: 10px;
    margin-bottom: 10px;
  }

  .list__wrapper {
    height: calc(100% - 77px) !important;
  }

  .pagination {
    text-align: right;
    position: static;
    margin-top: 10px;
  }

  .el-form-item {
    &:last-child {
      margin-bottom: 0;
    }
  }
}

::v-deep .jv-node {
  text-align: left;
}
</style>
